package com.hxd.otc.module.my.model;

import android.support.annotation.NonNull;

import com.hxd.otc.api.IOtcAdvertApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.common.enums.TakeOrderStatusEnum;
import com.hxd.otc.module.my.bean.AdvertBean;
import com.hxd.otc.module.my.bean.AdvertCoinTypeBean;
import com.hxd.otc.module.my.bean.AdvertPreBean;
import com.hxd.otc.module.my.bean.BaseListBean;
import com.hxd.otc.module.my.bean.MyBondBean;
import com.hxd.otc.module.my.bean.TakeOrderStatusBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.hxd.otc.utils.LogUtils;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by lzy on 2018/6/11 0011.
 * description ${广告Model}
 */
public class OtcAdvertModel {
    private OtcAdvertModel() {
    }

    private static OtcAdvertModel mModel = new OtcAdvertModel();

    public static OtcAdvertModel getInstance() {
        return mModel;
    }

    public void getMyAdvertList(int exType, int pageNo, int pageSize, BaseObserver<BaseListBean<AdvertBean>> observer,
                                AutoDisposeConverter<BaseBean<BaseListBean<AdvertBean>>> converter) {
        getOtcAdvertApi().getMyAdvertList(exType, pageSize, pageNo)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getADBond(BaseObserver<MyBondBean> observer, AutoDisposeConverter<BaseBean<MyBondBean>> converter) {
        getOtcAdvertApi().getADBond()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 获取币种信息
     */
    public void getCoinType(@NonNull BaseObserver<ArrayList<AdvertCoinTypeBean>> observer,
                            @NonNull AutoDisposeConverter<BaseBean<ArrayList<AdvertCoinTypeBean>>> converter, String userId) {
        getOtcAdvertApi()
                .getCoinType(userId, UserHelper.isNonInductivePaySilver() ? "0" : "1")//0无感支付，1普通交易
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 发布普通广告
     *
     * @param coinId              币种id
     * @param volume              币种数量
     * @param price               单价
     * @param symbol              币种符号
     * @param exType              买卖类型：0挂买    1挂卖
     * @param supportCurrencyCode 支持法币符号
     * @param supportBank         收款方式数组，用"，"隔开
     * @param minVolume           最小数量
     * @param maxVolume           最大数量
     * @param minQuota
     * @param maxQuota
     */
    public void publishAdvert(String coinId, String volume, String price, String symbol, int exType,
                              String supportCurrencyCode, String supportBank, String minVolume,
                              String maxVolume, int minQuota, int maxQuota, BaseObserver<String> observer,
                              AutoDisposeConverter<BaseBean<String>> converter) {
        getOtcAdvertApi().publishAdvert(volume, price, symbol, exType, supportCurrencyCode,
                supportBank, minVolume, maxVolume, minQuota, maxQuota)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 发布无感支付广告
     *
     * @param coinId              币种id
     * @param volume              币种数量
     * @param price               单价
     * @param symbol              币种符号
     * @param exType              买卖类型：0挂买    1挂卖
     * @param supportCurrencyCode 支持法币符号
     * @param supportBank         收款方式数组，用"，"隔开
     * @param minVolume           最小数量
     * @param maxVolume           最大数量
     * @param minQuota
     * @param maxQuota
     */
    public void publishEasyAdvert(String coinId, String volume, String price, String symbol, int exType,
                                  String supportCurrencyCode, String supportBank, String minVolume,
                                  String maxVolume, int minQuota, int maxQuota, BaseObserver<String> observer,
                                  AutoDisposeConverter<BaseBean<String>> converter) {
        getOtcAdvertApi().publishEasyAdvert(volume, price, symbol, exType, supportCurrencyCode,
                supportBank, minVolume, maxVolume, minQuota, maxQuota)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 发布浮动定价广告
     *
     * @param coinId              币种id
     * @param volume              币种数量
     * @param symbol              币种符号
     * @param exType              买卖类型：0挂买    1挂卖
     * @param supportCurrencyCode 支持法币符号
     * @param supportBank         收款方式数组，用"，"隔开
     * @param minVolume           最小数量
     * @param maxVolume           最大数量
     * @param floatPrice          如果是1浮动定价，下列参数必填
     * @param floatPlat           追踪平台
     * @param floatRate           浮动比例：0不浮动，正数上浮，负数下浮
     * @param minPrice            底价
     * @param minQuota
     * @param maxQuota
     */
    public void publishFloatAdvert(String coinId, String volume, String symbol, int exType,
                                   String supportCurrencyCode, String supportBank, String minVolume,
                                   String maxVolume, String floatPrice, String floatPlat, double floatRate,
                                   String minPrice, int minQuota, int maxQuota, BaseObserver<String> observer,
                                   AutoDisposeConverter<BaseBean<String>> converter) {
        getOtcAdvertApi().publishFloatAdvert(volume, symbol, exType, supportCurrencyCode, supportBank, minVolume,
                maxVolume, floatPrice, floatPlat, floatRate, minPrice, minQuota, maxQuota)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    /**
     * 下架广告
     *
     * @param orderId 广告id
     */
    public void revokeAdvert(String orderId, boolean isNosenseOrder, BaseObserver<String> observer,
                             AutoDisposeConverter<BaseBean<String>> converter) {

        if (isNosenseOrder) {
            getOtcAdvertApi().revokeEasePayAdvert(orderId)
                    .observeOn(AndroidSchedulers.mainThread())
                    .as(converter)
                    .subscribe(observer);
        } else {
            getOtcAdvertApi().revokeAdvert(orderId)
                    .observeOn(AndroidSchedulers.mainThread())
                    .as(converter)
                    .subscribe(observer);
        }

    }

    /**
     * 发布广告前置接口
     */
    public void getPublishAdvertPre(BaseObserver<HashMap<String, ArrayList<AdvertPreBean>>> observer,
                                    AutoDisposeConverter<BaseBean<HashMap<String, ArrayList<AdvertPreBean>>>> converter) {
        getOtcAdvertApi().getPublishAdvertPre()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 查询无感广告商接单状态
     *
     * @param observer
     * @param converter
     */
    public void tradeStutsByUserId(BaseObserver<TakeOrderStatusBean> observer,
                                   AutoDisposeConverter<BaseBean<TakeOrderStatusBean>> converter) {
        getOtcAdvertApi().tradeStutsByUserId()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 更新无感广告商接单状态
     *
     * @param observer
     * @param converter
     */
    public void updateTradeStuts(TakeOrderStatusEnum takeOrderStatusEnum, BaseObserver observer,
                                 AutoDisposeConverter<BaseBean> converter) {
        getOtcAdvertApi().updateTradeStuts(takeOrderStatusEnum.getStatus())
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public IOtcAdvertApi getOtcAdvertApi() {
        return RetrofitManager.getDefault().create(IOtcAdvertApi.class);
    }
}
